﻿using System;
using System.IO;
using System.Windows;
using Agnes.Core;
using System.ComponentModel;
using DevExpress.XtraRichEdit;

namespace Agnes.MyDocsLib
{
    /// <summary>
    /// Article 视图的ViewModel
    /// </summary>
    public sealed class ArticleViewModel
        :DocumentPanelBase<Article>
    {
        #region 绑定属性

        private bool _isEditMode = false;
        /// <summary>
        /// 是否处于编辑模式
        /// </summary>
        public  bool IsEditMode
        {
            get { return _isEditMode; }
            set 
            {
                _isEditMode = value;
                NotifyOfPropertyChange(() => IsEditMode);
            }
        } 

        #endregion

        #region 实现ScreenBase

        protected override object CreateView()
        {
            return new ArticleView();
        }

        #endregion

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="model"></param>
        public ArticleViewModel(Article model)
            : base(model)
        {
            Caption = model.Title;
            Model.PropertyChanged += (sender, args) =>
                                         {
                                             if (args.PropertyName == "Title")
                                             {
                                                 Caption = Model.Title;
                                             }
                                         };
            Model.BeginEdit();
            if (File.Exists(Model.DocumentPath))
            {
                (GetView(this) as ArticleView).recRichEdit.LoadDocument(Model.DocumentPath, DocumentFormat.Mht);
            }
        }


        public override void Cancel()
        {
            Model.CancelEdit();
        }

        public override void PreViewClose(object sender, CancelEventArgs e)
        {

        }

        public override void Save()
        {
            Model.ApplyEdit();
            (GetView(this) as ArticleView).recRichEdit.SaveDocument(Model.DocumentPath, DocumentFormat.Mht);
        }
    }
}
